/**
 * Project: lteerp
 * file:    src/main/BaseRequestProcessor.java
 *
 * the Main user Request Processor 
 *
 * in this class, we use a subclass of org.apache.struts.action.RequestProcessor
 * and in this class, we just do two things:
 * one, check one user whether had logined, if not, give a login page
 * secondly: check one user's privileges, if one user had not privileges, we just give a 
 * error page, said no privileges.
 * third:   record one user's any action
 *
 * @package:    com.m5anet.main
 * @author:     Mike.G  <hylinux@gmail.com> 黄叶
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: http://www.5anet.com
 * @version:    $Id: BaseRequestProcessor.java 7 2008-01-10 05:01:55Z hylinux $
 * @date:       $Date: 2008-01-10 05:01:55 +0000 (Thu, 10 Jan 2008) $
 */
package com.m5anet.main;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.util.ModuleUtils;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;



import java.io.IOException;


public class BaseRequestProcessor extends RequestProcessor {

    /**
     *  do one preprocessor
     *
     *  @param: req HttpServletRequest
     *  @param: res HttpServletResponse
     *  @return: boolean
     *  @access:    public
     */
    public boolean processPreprocess(
            HttpServletRequest req,
            HttpServletResponse res )  {
            
        boolean continueProcess = true;

        //get the Session
        HttpSession session = req.getSession();

        //get the user id
        int userId = 0;
        try {

            userId = ( ( Integer ) session.getAttribute("userId") ).intValue();

        } catch ( NumberFormatException ex) {
            userId = 0;
        }

        //if the userid is zero, so, we think the use don't login
        //our system. and then, we give that user one login page
        if ( userId == 0 ) {
            continueProcess = false;
            
            ForwardConfig forwardConfig = moduleConfig.findForwardConfig("noling");

            try {
                res.sendRedirect( forwardConfig.getPath());
            } catch ( IOException ex) {
                log.error("User can't login in, " + ex );
            }

        }

        return continueProcess;

    }


}

